using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

public static class UsefulExtensions
{
	public static T GetRandom<T>(this List<T> list)
	{
		return list[Random.Range(0, list.Count)];
	}

	public static List<T> GetRandomRange<T>(this List<T> list, int count)
	{
		List<T> list2 = new List<T>();
		list2.AddRange(list);
		List<T> list3 = new List<T>();
		while (list2.Count > 0 && list3.Count < count)
		{
			T random = list2.GetRandom();
			list2.Remove(random);
			list3.Add(random);
		}
		return list3;
	}

	public static void Hide(this CanvasGroup _canvas)
	{
		_canvas.interactable = false;
		_canvas.blocksRaycasts = false;
		_canvas.alpha = 0f;
	}

	public static void Show(this CanvasGroup _canvas)
	{
		_canvas.interactable = true;
		_canvas.blocksRaycasts = true;
		_canvas.alpha = 1f;
	}

	public static void SetActive(this List<GameObject> list, bool isActive)
	{
		foreach (GameObject item in list)
		{
			if (item != null)
			{
				item.SetActive(isActive);
			}
		}
	}

	public static async Task WaitSeconds(float seconds)
	{
		await Task.Delay((int)(seconds * 1000f));
	}

	public static List<T> RemoveEmpty<T>(this List<T> list)
	{
		for (int i = 0; i < list.Count; i++)
		{
			if (list[i] == null)
			{
				list.RemoveAt(i);
				i--;
			}
		}
		return list;
	}

	public static void SetTagsRecursive(this GameObject parent, string tag)
	{
		for (int i = 0; i < parent.transform.childCount; i++)
		{
			parent.transform.GetChild(i).gameObject.SetTagsRecursive(tag);
		}
		parent.tag = tag;
	}

	public static int Round(this int value, int factor)
	{
		if (value < factor)
		{
			return value;
		}
		return value / factor * factor;
	}

	public static int Round(this int value)
	{
		if (value < 10)
		{
			return value;
		}
		if (value < 100)
		{
			return value.Round(10);
		}
		if (value < 1000)
		{
			return value.Round(100);
		}
		if (value < 10000)
		{
			return value.Round(1000);
		}
		return value.Round(10000);
	}

	public static bool Choose(this float chance)
	{
		return Random.Range(0f, 100f) < chance;
	}

	public static float Range(this float range, float baseRange)
	{
		return Random.Range(baseRange, range);
	}

	public static float Range(this float range)
	{
		if (range > 0f)
		{
			return range.Range(0f);
		}
		return 0f.Range(range);
	}
}
